/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.bookingmanager.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.persistence.*;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

/**
 *
 * @author Jiří Holuša
 */
@Entity
@Table(name="users")
public class Administrator implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @Column(nullable=false, length=25)
    private String firstName;
    
    @Column(nullable=false, length=25)
    private String surName;
     
    @Column(name="username", nullable=false, length=25,unique=true)
    private String username;
    
    @Column(name="password", nullable=false, length=64)
    private String password;  
    
    @JoinColumn(nullable = false)
    @LazyCollection(LazyCollectionOption.TRUE)
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private List<AdminRole> roles;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getSurName() {
        return surName;
    }

    public void setSurName(String surName) {
        this.surName = surName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }    

    public Long getId() {
        return id;
    }   

    public void setId(Long id) {
        this.id = id;
    }
    
    public List<AdminRole> getRoles() {
        if (this.roles == null) {
            return null;
        }

        return Collections.unmodifiableList(this.roles);
    }

    public void setRoles(List<AdminRole> roles) {
        this.roles = null;

        if (roles != null) {
            this.roles = new ArrayList<AdminRole>();
            this.roles.addAll(roles);
        }
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {        
        if (!(object instanceof Administrator)) {
            return false;
        }
        Administrator other = (Administrator) object;
        if ((this.id == null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Administrator{" + "id=" + id + ", firstName=" + firstName + ", surName=" + surName + '}';
    }    
    
}
